SQL MIN function returns least or minimum value out of available records/values. Oracle MIN Function is opposite of the SQL MAX Function.
SQL MIN Function Syntax
SELECT MIN(expression) FROM table_name WHERE conditions;
SQL MIN Function Examples
Let’s take an example for understanding the Oracle MIN Function
Suppose we have a table named “Employee” with the data as shown below.
Employee_ID | Employee_Name | Salary | Department | Commission |
101 | Emp A | 10000 | Sales | 10 |
102 | Emp B | 20000 | IT | 20 |
103 | Emp C | 28000 | IT | 20 |
104 | Emp D | 30000 | Support | |
105 | Emp E | 32000 | Sales | 10 |
Now we will see what the SQL MIN Function does when used in different scenarios
SQL MIN Function – Simple Usage
The simplest way to use SQL MIN Function is to fetch the minimum value from a column of a table
For example, with the below Oracle MIN Function query we will be able to fetch the minimum value of commission.
SELECT MIN(commission) as "MIN Commission" FROM employee;
The above MIN SQL Function query will return result as “10” as that’s the lowest commission available in the “Employee” table.
Also note that we have aliased the MIN(commission) field as “MIN Commission”, hence once the query is run “MIN Commission” will be displayed as the field name of the result from the Oracle MIN Function query.
NULL values are not considered by the MIN SQL Function.
SQL MIN Function – SQL WHERE Clause Example
SQL MIN Function can be used in SQL SELECT Statement having SQL WHERE Clause.
For example, below SQL MIN query returns minimum salary in ‘Sales’ department.
SELECT MIN(salary) as "Minimum Salary" FROM employee WHERE department = 'Sales';
The above Oracle MIN Function query returns ‘10000’ as minimum salary in ‘Sales’ department.
Also note that we have aliased the MIN(salary) field as “Minimum Salary”.
SQL MIN Function – SQL GROUP BY Clause Example
SQL MIN Function can be used in SQL SELECT Statement having SQL GROUP BY Clause.
For example, Oracle MIN Function query below, shows department name and minimum salary of each department.
SELECT department "Department Name" ,MIN(salary) "Minimum Salary" FROM employee GROUP BY department;
The data returned by above MIN SQL Function query will be:
Departement Name | Minimum Salary |
Sales | 10000 |
IT | 20000 |
Support | 30000 |
Here notice that by using Oracle MIN Function with SQL GROUP BY Clause we have retrieved minimum salaries of every unique department along with the department name.